#! /usr/local/bin/python

# Web 2.0

import cgitb; cgitb.enable()
import cgi
import urllib
import urllib2
import sys

# get form variables (if desired)
form = cgi.FieldStorage()

passwd = form["password"].value
username = form["username"].value
title = form["title"].value
content = form["content"].value

blogurl  = 'http://www.cs.cityu.edu.hk/~cs4380/webservice/blog/createPost.py'

# setup the password
password_manager = urllib2.HTTPPasswordMgrWithDefaultRealm()
password_manager.add_password(None, 'http://www.cs.cityu.edu.hk/', username, passwd)
auth_handler = urllib2.HTTPBasicAuthHandler(password_manager)
opener = urllib2.build_opener(auth_handler)
urllib2.install_opener(opener)

# setup the parameters for calling the web service
params = urllib.urlencode({
  'title': title, 
  'content': content
})

# call the webservice
data = urllib2.urlopen(blogurl, params).read()

# print out the content header
print 'Content-type: text/html\n\n'

print data

